package com.qnw.CardGroupManagement.fragment;
import android.os.*;
import android.support.v4.app.*;
import android.view.*;
import com.ygo.feihua.*;
import android.widget.*;
import android.view.View.*;
import com.ygo.feihua.util.*;
import java.util.*;
import com.ygo.feihua.adapter.*;
import android.widget.AdapterView.*;
import android.view.inputmethod.*;
import android.util.*;
import com.qnw.CardGroupManagement.*;

public class FragmentImageList extends Fragment implements OnClickListener
{

	public boolean isSelected()
	{
		// TODO: Implement this method
		return adpt.isSelected();
	}
	TextView tv;
	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.ci_del:
				if(xzci.size()!=0){
					View[] vv=du.dialogj1(false,"","删除中");
					tv=(TextView) vv[0];
					new Thread(new Runnable(){

							@Override
							public void run()
							{
								int a=0;
								
								int b=xzci.size();
								for(int i=0;i<b;i++){
									String s=xzci.get(i);
								//	Log.e("del","删除中"+i+"/"+b);
									Message me=new Message();
									me.what=2;
									
									me.obj="删除中"+(i+1)+"/"+b;
									han.sendMessage(me);
								 String ss=ciu.DelCardImage(CardImageUtil.TYPE_PATH,s);
								 if(ss.equals("")){
								 
								 ci.remove(s);
								 
								 }else{
									a++;
								 }
								 }
								Message me1=new Message();
								me1.what=3;
								if(a==0){
									me1.obj="删除完成";
								}else{
									me1.obj="删除完成,删除失败"+a+"张";
								}
								han.sendMessage(me1);
								 
								 
								// TODO: Implement this method
							}
						}).start();
					}else{
					gj.show("至少选择一张卡图");
				}
				break;
			case R.id.ci_qx:
				if(xzci.size()!=ci.size()){
				for(String s:ci){
					if(!xzci.contains(s)){
						xzci.add(s);
					}
				}
				adpt.notifyDataSetChanged();
				}
				break;
		}
		
		
		// TODO: Implement this method
	}

	
	
	private ListView fal_list;
	private Button fal_cx;
	private EditText fal_name;
	private CardImageUtil ciu;
	private Gongju gj;
	private AdapterCardImage adpt;
	private List<String> ci;
	private List<String> xzci;
	private LinearLayout fal_lin;
	private View op_ci;
	private J5 j5;
	private DialogUtils du;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_cardimage_list,null);

		initView(v);

		// TODO: Implement this method
		return v;
	}

	private void initView(View v)
	{
		fal_cx=(Button) v.findViewById(R.id.fal_cx);
		fal_list=(ListView) v.findViewById(R.id.fal_list);
		fal_name=(EditText) v.findViewById(R.id.fal_name);
		fal_lin=(LinearLayout) v.findViewById(R.id.fal_lin);
		
		j5=(J5) getActivity();
		du=DialogUtils.getdx(getActivity());
		
		op_ci=LayoutInflater.from(getActivity()).inflate(R.layout.operation_cardimage,null);
		
		LinearLayout ci_del,ci_qx;
		ci_del=(LinearLayout) op_ci.findViewById(R.id.ci_del);
		ci_qx=(LinearLayout) op_ci.findViewById(R.id.ci_qx);
		
		ci_del.setOnClickListener(this);
		ci_qx.setOnClickListener(this);
		xzci=new ArrayList<String>();
		ci=new ArrayList<String>();
		gj=Gongju.getdx(getActivity());
		ciu=new CardImageUtil(SharedPreferenceUtil.getYgopath()+"pics.zip");
		adpt=new AdapterCardImage(getActivity(),ci,xzci);
		fal_list.setAdapter(adpt);
		fal_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					if(isSelected()){
						if(xzci.contains(ci.get(p3))){
							xzci.remove(ci.get(p3));
						}else{
						xzci.add(ci.get(p3));
						}
						adpt.notifyDataSetChanged();
					}else{
						j5.setkt(CardImageUtil.TYPE_PATH,ci.get(p3));
						j5.closeDrawer();
					}
					
					// TODO: Implement this method
				}
			});
			
		fal_list.setOnItemLongClickListener(new OnItemLongClickListener(){

				@Override
				public boolean onItemLongClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					if(!adpt.isSelected()){
						setselected(true);
						xzci.add(ci.get(p3));
						adpt.notifyDataSetChanged();
					}				
					// TODO: Implement this method
					return true;
				}
			});
		//设置回车搜索
		fal_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
					if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {

						String km = fal_name.getText().toString().trim();
					sskt(km);
						
						return true;
					}
					return false;
				}
			});
		
		fal_cx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String km=fal_name.getText().toString().trim();
					sskt(km);
					// TODO: Implement this method
				}
			});
		
		// TODO: Implement this method
	}
	
	Handler han=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					du.dis();
					adpt.notifyDataSetChanged();
					break;
				case 1:
					gj.show("未查询到卡图");
					break;
				case 2:
					tv.setText((String)msg.obj);
					break;
				case 3:
					gj.show((String)msg.obj);
					setselected(false);
					du.dis();
					break;
			}
			
			
		}
		
		
		
	};
	
	public void sskt(final String s){
		du.dialogj1(false,"","查询卡图中,请稍等");
		new Thread(new Runnable(){

				@Override
				public void run()
				{
					ci.clear();
					if(!s.equals("")){
						List<String> c=ciu.CIQuery(s);
					
						if(c.size()==0){
					
							Message me=new Message();
							me.what=1;
							han.sendMessage(me);
						}else{
							for(String s:c){
								ci.add(s);
							}
						}
				
					}
					Message me=new Message();
					me.what=0;
					han.sendMessage(me);
					
					// TODO: Implement this method
				}
			}).start();
		
	}
	
	public void setselected(boolean b){
		if(adpt.isSelected()!=b){
		adpt.setSelected(b);
		if(b){
			fal_lin.addView(op_ci);
		}else{
			fal_lin.removeView(op_ci);
		}
		}
	}
	
}
